<?php

namespace app\controllers;

use Yii;
use app\models\Mobie;
use app\models\MobieSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

use Qiniu\Auth;

/**
 * MobieController implements the CRUD actions for Mobie model.
 */
class MobieController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all Mobie models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new MobieSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    
    public function actionAjax($tab=1)
    {
       $sql = "select * from orders where status = 0 and order_type = ".$tab." order by order_sort desc;";
       
       $data = Yii::$app->db->createCommand($sql)->queryAll();

       echo json_encode($data);
    }

    public function actionBannermajax()
    {
        $sql = "select * from banner limit 1";
        $data = Yii::$app->db->createCommand($sql)->queryAll();
       echo json_encode($data);
    }


    public function  actionToken(){
        // require_once __DIR__ . './../autoload.php';

        

        // $accessKey = 'bIEEmQ3ZrjrQ0c44TRn4t-kl--2xN4fhz7LclsWZ';
        // $secretKey = 'Eosr0D-TnXS6pBheDVlfWjh-RoXu5P-jiUnwSgYv';
        // $auth = new Auth($accessKey, $secretKey);

        // $bucket = 'Bucket_Name';
        // $upToken = $auth->uploadToken($bucket);
        // echo $upToken;

        $xml = file_get_contents("https://sslsharecircle.51yund.com/getQiniuToken?source=city_activity");
        echo $xml;

    }

    public function actionImageurl($filename,$source){
        
        $xml = file_get_contents("https://sslsharecircle.51yund.com/getQiniuImgUrl?filename=".$filename."&source=".$source);
        echo $xml;

    }





    /**
     * Displays a single Mobie model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Mobie model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Mobie();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->order_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing Mobie model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->order_id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing Mobie model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the Mobie model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Mobie the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Mobie::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
